const path = require('path')
const fs = require('fs')
const HTMLPlugin = require('html-webpack-plugin')
const cookieFile = path.resolve(__dirname, '.auth.cookie')

// for remote api
// const TARGET_SERVER = 'http://172.20.4.177:8080/'
const TARGET_SERVER = 'http://172.20.4.119:8080/'
// for build output
// const EMAP_ROOT = '/home/qtx/workspace/EMAP/eetablecore/web/'
const EMAP_ROOT = 'dist'
const EMAP_PROJECT = 'form_render'
const isDev = process.env.NODE_ENV === 'development'

// console.log('env === ', process.env.NODE_ENV)

function resolve (dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    lintOnSave: true,
    publicPath: !isDev ? `./<%= coreRoot %>/${EMAP_PROJECT}/` : '/',
    outputDir: !isDev ? path.join(EMAP_ROOT, EMAP_PROJECT) : 'dist',
    configureWebpack: config => {
        config.devtool = 'source-map'
        config.optimization && (config.optimization.splitChunks.minSize = 10000)

        config.entry = {
            index: './src/main.js',
            mobile: './src/mobile.js',
            form: './src/form.js',
            form_mobile: './src/form_mobile.js',
        }

        config.resolve.alias = {
            'components': resolve('src/components'),
            'controls': resolve('src/controls'),
            'utils': resolve('src/utils'),
            'pages': resolve('src/pages'),
            'services': resolve('src/services'),
            'bh-vue': resolve('src/lib/bh-vue'),
            'mock': resolve('src/mock'),
            'mobile': resolve('src/mobile'),
            'mb-vue': resolve('src/mobile/components')
        }

        if (isDev) {
            config.plugins.push(
                new HTMLPlugin({
                    entry: 'index',
                    chunks: ['chunk-vendors', 'chunk-common', 'index'],
                    template: './public/index.html',
                    filename: 'index.html'
                }),
                new HTMLPlugin({
                    entry: 'mobile',
                    chunks: ['chunk-vendors', 'chunk-common', 'mobile'],
                    template: './public/mobile.html',
                    filename: 'mobile.html'
                }),
                new HTMLPlugin({
                    entry: 'form',
                    chunks: ['chunk-vendors', 'chunk-common', 'form'],
                    template: './public/form.html',
                    filename: 'form.html'
                }),
                new HTMLPlugin({
                  entry: 'form_mobile',
                  chunks: ['chunk-vendors', 'chunk-common', 'form_mobile'],
                  template: './public/form_mobile.html',
                  filename: 'form_mobile.html'
              }),
            )
        } else {
            config.plugins.push(
                new HTMLPlugin({
                    entry: 'index',
                    chunks: ['chunk-vendors', 'chunk-common', 'index'],
                    template: 'raw-loader!./public/template.jsp',
                    filename: path.join(__dirname, EMAP_ROOT, 'index.jsp')
                }),
                new HTMLPlugin({
                    entry: 'mobile',
                    chunks: ['chunk-vendors', 'chunk-common', 'mobile'],
                    template: 'raw-loader!./public/index_mobile.jsp',
                    filename: path.join(__dirname, EMAP_ROOT, 'index_mobile.jsp')
                }),
                new HTMLPlugin({
                    entry: 'form',
                    chunks: ['chunk-vendors', 'chunk-common', 'form'],
                    template: 'raw-loader!./public/form.jsp',
                    filename: path.join(__dirname, EMAP_ROOT, 'form.jsp')
                }),
                new HTMLPlugin({
                  entry: 'form_mobile',
                  chunks: ['chunk-vendors', 'chunk-common', 'form_mobile'],
                  template: 'raw-loader!./public/form_mobile.jsp',
                  filename: path.join(__dirname, EMAP_ROOT, 'form_mobile.jsp')
              }),
            )
        }
    },
    devServer: {
        port: 3001,
        open: false,
        // 跨域
        proxy: {
            '/emap/': {
                // target: 'http://gemini.wisedu.com/gemini_dev',
                target: TARGET_SERVER,
                changeOrigin: true,
                bypass: (req) => {
                    const cookieContent = fs.readFileSync(cookieFile, 'utf-8')
                    req.headers = req.headers || {}
                    req.headers.referer = TARGET_SERVER
                    req.headers.cookie = cookieContent
                },
            },
            '/sys/emapflow/': {
                target: TARGET_SERVER,
                pathRewrite: {'^/sys/emapflow/' : '/emap/sys/emapflow/'},
                changeOrigin: true,
                bypass: (req) => {
                    const cookieContent = fs.readFileSync(cookieFile, 'utf-8')
                    req.headers = req.headers || {}
                    req.headers.referer = TARGET_SERVER
                    req.headers.cookie = cookieContent
                },
            },
            // '/sys/emapturing/getConfig.do': {
            //     //   target: TARGET_SERVER,
            //     //   pathRewrite: {'^/sys/emapturing/' : '/emap/sys/emapturing/'},
            //     //   changeOrigin: true,
            //     bypass: (req) => {
            //     //   const cookieContent = fs.readFileSync(cookieFile, 'utf-8')
            //         req.headers = req.headers || {}
            //         req.headers.referer = TARGET_SERVER
            //         // req.headers.cookie = cookieContent
            //         req.headers.cookie = 'JSESSIONID=10FDCCE640A5CDB119D97ADA24CE8BAB'
            //         req.headers.referer= "http://172.20.4.119:8080/emap/sys/emapfunauth/pages/funauth-login.do?service=http%3A%2F%2F172.20.4.119%3A8080%2Femap%2Fsys%2Femapturing%2FgetConfig.do"
            //     },
            // },
            '/sys/eetablecore/': {
              target: TARGET_SERVER,
              pathRewrite: {'^/sys/eetablecore/' : '/emap/sys/eetablecore/'},
              changeOrigin: true,
              bypass: (req) => {
                //   const cookieContent = fs.readFileSync(cookieFile, 'utf-8')
                  req.headers = req.headers || {}
                  req.headers.referer = TARGET_SERVER
                  // req.headers.cookie = cookieContent
                  req.headers.cookie = 'JSESSIONID=10FDCCE640A5CDB119D97ADA24CE8BAB'
              },
            }
        },
        before (apiRoutes) {
            apiRoutes.get('/sys/emapturing/getConfig.do', (req, res) => {
                res.json({
                    USERINFO: {
                        userImage: 'http://res.wisedu.com/scenes/public/images/demo/user1.png',
                        logoutUrl: "/emap/sys/emapfunauth/logout.do",
                        userName: "管理员"
                    }
                })
            })
        }
    }
}
